package com.tsmti.lab.controller;

import com.tsmti.core.util.CookieUtils;
import com.tsmti.core.util.PropertiesUtils;
import com.tsmti.core.util.StringUtils;
import com.tsmti.util.UserUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * @Package：com.tsmti.lab.controller.system
 * @Class：UserController
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/3/14 17:15
 * @Company:
 * @Version：
 * @Modified By:
 */
@Controller
public class IndexController {

	@RequestMapping(value = "/index" , method = RequestMethod.GET)
	public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
		// 加载菜单
		model.addAttribute("menus", UserUtils.getMenuList());
		// 加载模版
//		String theme = getTheme(request);
		return "home";
	}

	/**
	 * 加载风格
	 * 
	 * @title: getTheme
	 * @description: TODO(这里用一句话描述这个方法的作用)
	 * @param request
	 * @return
	 * @return: String
	 */
	private String getTheme(HttpServletRequest request) {
		// 默认风格
		PropertiesUtils propertiesUtil = new PropertiesUtils("app.properties");
		String theme = propertiesUtil.getString("default.theme");
		if (StringUtils.isEmpty(theme)) {
			theme = "uadmin";
		}
		// cookies配置中的模版
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			if (cookie == null || StringUtils.isEmpty(cookie.getName())) {
				continue;
			}
			if ("theme".equalsIgnoreCase(cookie.getName())) {
				theme = cookie.getValue();
			}
		}
		return theme;
	}

	/**
	 * Coookie设置
	 */
	@RequestMapping(value = "/theme/{theme}")
	public String getThemeInCookie(@PathVariable String theme, HttpServletRequest request,
			HttpServletResponse response) {
		if (StringUtils.isNotBlank(theme)) {
			CookieUtils.setCookie(response, "theme", theme);
		} else {
			theme = CookieUtils.getCookie(request, "theme");
		}
		return "redirect:" + request.getParameter("url");
	}

	@RequestMapping("/welcome")
	public String welcome() {
		return "welcome";
	}

	@RequestMapping("/lock")
	public String lock() {
		return "lock";
	}

}
